Add assertThrows variants which accept exceptions as values#5819
Add assertThrows variants which accept exceptions as values#5819mdedetrich wants to merge 1 commit into
Conversation
9c94c33 to
e44a582
Compare
e44a582 to
d2c0d8e
Compare
|
@mdedetrich Can't you use |
d2c0d8e to
95ba98b
Compare
Not with a kotlin suspend function that you are asserting, thats the precise issue. It will fail to compile for the reasons I stated in the PR. Kotlin suspend only works if the Of course you could just do Assertions.assertThrows(IllegalArgumentException::class.java) {
runBlocking {
someKotlinSuspendFunc()
}
}But one of the whole point of I have just rebased the PR against latest main to resolve conflicts. |
Sorry for making you re-explain it. I didn't read carefully enough. 😬 |
Signed-off-by: Matthew de Detrich <matthew.dedetrich@gls-itservices.com>
95ba98b to
d3494e9
Compare
|
I wonder whether having the reified and the new variants is confusing and we should deprecate the old ones. WDYT? 🤔 |
I personally see value in both and actually prefer the original variants. Its just that there are some cases where you are using exceptions as values (i.e. While its true that there are a lot of overloads, the design is intelligent in that the ones that use exception values always have exception as first arg so the quality of life of users with IDE's should be a non issue (talking about ergonomics of finding the right overload). tld;r I think its a non issue and there is value in both cases, like generally with JVM languages in some cases you treat types as values and in other cases as type parameters. |
✅ All tests passed ✅🏷️ Commit: d3494e9 Learn more about TestLens at testlens.app. |
Currently there exists
org.junit.jupiter.api.assertThrowsand its various variants to be used with JUnit and Kotlin that are also compatible with coroutines (i.e.suspend func). The current design uses reified inline functions, which generally work aside from one situation, specifically@ParameterizedTestthat produces exceptions classes as values and you intend to useassertThrowson those exception values.In this case, the current
org.junit.jupiter.api.assertThrowsdoesn't work because in order to use it you would have to make your test function (the one annotated with@ParameterizedTest) aninline funcwith a reifiedT: Throwablehowever this won't be picked up by JUnit as@ParameterizedTestuses reflection.This PR adds variants of the currently existing
org.junit.jupiter.api.assertThrowsthat have anexpectedType: Class<T>as the first parameter (which is the same convention asorg.junit.jupiter.api.Assertions.assertThrows) but since these added variants areinline fun, functions/methods used in the body can besuspend func(this is verified with the added tests).The current implementation is a bit of boilerplate however this is inevitable to a degree since we cannot use the currently existing
org.junit.jupiter.api.assertThrowsand its variants implementations as they only accept reifiedT: Throwablewhich then defeats the purpose of what we are trying to achieve.I hereby agree to the terms of the JUnit Contributor License Agreement.
Definition of Done
@APIannotations